package com.shine.minepressure.ui.login;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.shine.minepressure.R;
import com.shine.minepressure.app.Constants;
import com.shine.minepressure.base.BaseActivity;
import com.shine.minepressure.bean.LoginBean;
import com.shine.minepressure.databinding.ActivityLoginNewBinding;
import com.shine.minepressure.http.ApiException;
import com.shine.minepressure.http.ECObserver;
import com.shine.minepressure.http.RetrofitClient;
import com.shine.minepressure.http.RxUtils;
import com.shine.minepressure.http.Service;
import com.shine.minepressure.ui.main.MainActivity;
import com.shine.minepressure.utils.SPUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class LoginNewActivity extends BaseActivity<ActivityLoginNewBinding, LoginNewViewModel> {
    private int SET_IP = 1001;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login_new;
    }

    @Override
    public int initVariableId() {
        return com.shine.minepressure.BR.viewModel;
    }

    @Override
    public LoginNewViewModel initViewModel() {
        return new LoginNewViewModel(this);
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData() {
        super.initData();

        binding.userName.setText(SPUtils.getInstance().getString(Constants.SP_LOGIN_NAME, ""));
        binding.passWord.setText(SPUtils.getInstance().getString(Constants.SP_LOGIN_PASS, ""));

        if (TextUtils.isEmpty(SPUtils.getInstance().getString(Constants.SP_IP, ""))) {
            switchActivityForResult(SetIpActivity.class, SET_IP, null);
        }

//        "http://172.16.19.245:8100/DataFactotyWebHttp/"

//        SPUtils.getInstance().clear();

//        SPUtils.getInstance().put(Constants.SP_LOGIN_NAME, binding.userName.getText().toString());
//        SPUtils.getInstance().put(Constants.SP_LOGIN_PASS, binding.passWord.getText().toString());

        binding.icon.setOnClickListener(this);
        binding.login.setOnClickListener(this);
        binding.changeIp.setOnClickListener(this);

        RxPermissions mRxPermission = new RxPermissions(this);
        mRxPermission.request(Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {

                        } else {
                        }
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                if (TextUtils.isEmpty(SPUtils.getInstance().getString(Constants.SP_IP, ""))) {
                    ToastMessage("请先设置IP地址");
                    return;
                }

                if (binding.userName.getText().toString().isEmpty()) {
                    ToastMessage("请输入您的用户名");
                    return;
                }

                if (binding.passWord.getText().toString().isEmpty()) {
                    ToastMessage("请输入密码");
                    return;
                }

//                switchActivity(MainActivity.class);
                loginByPassword();
                break;
            case R.id.changeIp:
                switchActivityForResult(SetIpActivity.class, SET_IP, null);
                break;
            case R.id.icon:

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == SET_IP) {
                switchActivityAndFinish(MainActivity.class);
            }
        }
    }

    private void loginByPassword() {
        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .LoginAuthentication(binding.userName.getText().toString().trim(), binding.passWord.getText().toString().trim())
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<LoginBean>() {
                    @Override
                    protected void _onNext(LoginBean loginBean) {
                        dismissLoading();
                        if (loginBean.getRows().getLoginState().equals("False")) {
                            ToastMessage("登录失败");
                            return;
                        }

                        SPUtils.getInstance().put(Constants.SP_LOGIN_NAME, binding.userName.getText().toString());
                        SPUtils.getInstance().put(Constants.SP_LOGIN_PASS, binding.passWord.getText().toString());
                        switchActivity(MainActivity.class);
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }


    private void loginSuccess(LoginBean loginBean) {
//        if (isRememberPassword) {
//            SPUtils.getInstance().put(Constants.SP_IS_REMEMBER_PASSWORD, true);
//        } else {
//            SPUtils.getInstance().put(Constants.SP_IS_REMEMBER_PASSWORD, false);
//        }
//        SPUtils.getInstance().put(Constants.SP_TOKEN, loginBean.getToken());

//        LoginBeanCookie loginBeanCookie = new LoginBeanCookie();
//        loginBeanCookie.setId(loginBean.getId());
//        loginBeanCookie.setMobile(loginBean.getMobile());
//        loginBeanCookie.setVipLevel(loginBean.getVipLevel());
//        SPUtils.getInstance().put(Constants.SP_COOKIE, new Gson().toJson(loginBeanCookie));

    }


}
